from django.shortcuts import render

# Create your views here.
from rest_framework.generics import ListAPIView, RetrieveAPIView
from . import models
from .serializers import CourseCategoryModelSerializer, CourseModelSerializer, CourseDetailModelSerializer,ChapterModelSerializer

class CourseCategoryView(ListAPIView):
    queryset = models.CourseCategory.objects.filter(is_show=True,is_deleted=False)
    serializer_class = CourseCategoryModelSerializer


from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.pagination import PageNumberPagination

class CustomPagenation(PageNumberPagination):
    page_size = 5  #没页显示5条记录
    page_query_param = 'page'  #?page=2
    page_size_query_param = 'size'  # /list/?size=20
    max_page_size = 100  #最大每页显示几条

class CourseListView(ListAPIView):

    queryset = models.Course.objects.filter(is_show=True,is_deleted=False).order_by('id')
    serializer_class = CourseModelSerializer
    filter_backends = [DjangoFilterBackend, ]
    filter_fields = ('course_category',)
    pagination_class = CustomPagenation

# /list/?course_category=2


class CourseDetailListView(RetrieveAPIView):
    queryset = models.Course.objects.filter(is_show=True,is_deleted=False)
    serializer_class = CourseDetailModelSerializer



class ChapterListView(ListAPIView):

    queryset = models.CourseChapter.objects.filter(is_show=True,is_deleted=False)
    serializer_class = ChapterModelSerializer
    filter_backends = [DjangoFilterBackend, ]
    filter_fields = ('course',)
    #  /course/chapter/?course=2



